package com.quanyan.user.web;

import com.quanyan.api.APIResponse;
import com.quanyan.user.constants.UserConstants;
import com.quanyan.user.model.vo.request.ReqUserInterest;
import com.quanyan.user.service.UserInterestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by yue.yuan on 2016/6/29.
 */
@RestController
@RequestMapping("/api/user/interest")
public class UserInterestController
{

    @Autowired
    private UserInterestService userInterestService;

    /**
     * 获取兴趣列表
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public APIResponse queryInterestList()
    {
        return userInterestService.selectInterestsByExample();
    }

    /**
     * 获取我的兴趣列表
     * @param request
     * @param reqUserInterest
     * @return
     */
    @RequestMapping(value = "/my", method = RequestMethod.POST,produces="application/json")
    public APIResponse queryUserInterestList(HttpServletRequest request,@RequestBody @Valid ReqUserInterest reqUserInterest)
    {
        Integer uid = 0;
        //1游客，2注册用户
        Integer userType = reqUserInterest.getUserType();
        if (userType.equals(UserConstants.USER_REGISTER)) {
            uid = (Integer) request.getAttribute("uid");
        }
        return userInterestService.selectUserInterestsByExample(reqUserInterest, uid);
    }

    /**
     * 更新我的兴趣列表
     * @param request
     * @param reqUserInterest
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public APIResponse updateUserInterestList(HttpServletRequest request,@RequestBody @Valid ReqUserInterest reqUserInterest)
    {
        Integer uid = 0;
        //1游客，2注册用户
        Integer userType = reqUserInterest.getUserType();
        if (userType.equals(UserConstants.USER_REGISTER)) {
            uid = (Integer) request.getAttribute("uid");
        }
        return userInterestService.updateInterests(reqUserInterest, uid);
    }
}
